package com.zrrd.jt_admin.image.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.jt_admin.common.entity.Image;
import com.zrrd.jt_admin.file.service.FileStorageService;
import com.zrrd.jt_admin.image.mapper.ImageMapper;
import com.zrrd.jt_admin.image.service.ImageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wanjl
 * @since 2024-07-03
 */
@Service
@Slf4j
public class ImageServiceImpl extends ServiceImpl<ImageMapper, Image> implements ImageService {

    @Resource
    private FileStorageService fileStorageService;

    /**
     * 删除图片
     *
     * @param id
     * @return
     */
    @Override
    public boolean remove(Integer id) {
        Image image = getById(id);
        //删除MinIO中的文件
        fileStorageService.deleteFile(image.getPath());
        //删除数据库里的图片数据
        boolean flag = removeById(id);
        return flag;
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public boolean romoveAll(List<Integer> ids) {
        //ids.forEach(this::remove);
        log.info("批量删除");
        for (Integer id : ids) {
            boolean flag = remove(id);
            if (!flag){
                return false;
            }
        }
        return true;
    }
}
